package com.example.geange.demo01;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by geange on 16-7-14.
 */
public class NewsAdapter extends BaseAdapter {

    private List<NewsItem> newsItems;
    private LayoutInflater mInflater;

    public NewsAdapter(List<NewsItem> newsItems, Context mContext) {
        this.newsItems = newsItems;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return newsItems.size();
    }

    @Override
    public Object getItem(int i) {
        return newsItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null){
            viewHolder = new ViewHolder();
            view = mInflater.inflate(R.layout.news_line, null);
            viewHolder.picture = (ImageView) view.findViewById(R.id.news_img);
            viewHolder.title = (TextView) view.findViewById(R.id.news_title);
            viewHolder.time = (TextView) view.findViewById(R.id.news_time);
            viewHolder.content = (TextView) view.findViewById(R.id.news_content);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.picture.setImageResource(R.drawable.fail);
        String url = newsItems.get(i).getPictureUrl();
        viewHolder.picture.setTag(url);
        new ImageLoader().showImageByThread(viewHolder.picture, url);
        viewHolder.title.setText(newsItems.get(i).getTitle());
        viewHolder.time.setText(newsItems.get(i).getTime());
        viewHolder.content.setText(newsItems.get(i).getContent());

        return view;
    }

    class ViewHolder{
        ImageView picture;
        TextView title;
        TextView time;
        TextView content;
    }
}
